package com.lvwenguo.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/*
* jdbcutil工具 类，单例模式
* */
public class JdbcUtil {

    private String driverClass;
    private String url;
    private String user;
    private String password;

    private static JdbcUtil jdbcUtil;
    private Connection con;
    /*
    * 私有化构造方法
    * */
    private JdbcUtil(){}
    /*
     * 获取实例化对象，单例模式
     * */
    public static JdbcUtil getJdbcUtil(){

        if(jdbcUtil==null){
            /*
            * 使用同步锁，防止多线程下对象实现多例化
            * */
            synchronized(JdbcUtil.class){
                if(jdbcUtil==null){
                    jdbcUtil=new JdbcUtil();
                }
            }
        }
        return jdbcUtil;
    }

    public Connection getConnertion(){
        return con;
    }
    public void initGetConnertion(Properties properties){
        driverClass=properties.getProperty("driver-Class");
        url=properties.getProperty("url");
        user=properties.getProperty("user");
        password=properties.getProperty("password");
        System.out.println(driverClass);
        System.out.println(url);
        System.out.println(user);
        System.out.println(password);
        initConnertion();
    }
    public void initConnertion(){
        try {
            Class.forName(driverClass);
            con= DriverManager.getConnection(url,user,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
